package com.myproject.io.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/*
* 缺点：
* 1、IO代码里read操作是阻塞操作，如果连接不做数据读写操作会导致线程阻塞，浪费资源
* 2、如果线程很多，会导致服务器线程太多，压力太大，比如C10K问题
* */
public class SocketServer {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        while(true){
            System.out.println("等待连接。。。");
            Socket clientSocket = serverSocket.accept();
            System.out.println("有客户端连接了。。。");
            handler(clientSocket);

            //可使用线程或线程池处理
            /*new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler(clientSocket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();*/
        }
    }

    private static void handler(Socket clientSocket) throws IOException {

        byte[] bytes = new byte[1024];
        System.out.println("准备read。。。");
        //接收客户端数据，阻塞方法，没有数据可读时就阻塞
        int read = clientSocket.getInputStream().read(bytes);
        System.out.println("read完毕。。。");
        if (read!=-1){
            System.out.println("接收到客户端的数据：" + new String(bytes,0,read));
        }
        clientSocket.getOutputStream().write("HelloClient".getBytes());
        clientSocket.getOutputStream().flush();
    }
}
